<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData,subOnLoad, formValidation, changeEditor,configSelectData} from '../../../static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,sumF, getDateStr} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				initValue:{
					posId: "",
					instId: "",
					fileList:[]//上次保存的文件列表
				},
				businessformCacheKey:'fin-bx-expenses-data-info-businessform-cache',
				instCacheKey:'fin-bx-expenses-data-info-inst-cache',
				accountCacheKey:'fin-bx-expenses-data-info-account-cache',
				bxUserId:'',
			})
		},
		watch:{
			formdata:{
				handler(newValue,oldValue){
					let _that = this.$refs.autoform
					_that.formdata.sumCosts = sumF(
						[
							this.formdata.longCosts,
							this.formdata.longCosts1,
							this.formdata.shortCosts,
							this.formdata.stayCosts,
							this.formdata.eatCosts,
							this.formdata.subsidyCosts,
							this.formdata.tollCosts,
							this.formdata.oilCosts,
							this.formdata.otherCosts
						]
					)
				},
				deep:true
			},
		},
		methods: {
			clearBusinessTravel(){
				let formdata = this.$refs.autoform.formdata
				formdata.businessTravelId = "";
				formdata.businessTravelDataId = "";
				formdata.businessTravelCode = "";
				formdata.planStartTime = "";
				formdata.planEndTime = "";
				formdata.startTime = "";
				formdata.endTime = "";
				formdata.cityName = "";
				formdata.address = "";
				formdata.vehicleTypeId = "";
				formdata.vehicleTypeName = "";
			},
			onSelectAccount(account){
				this.$refs.autoform.formdata.accountCode = account.code;
				if(account.layerNum == 1) {
					this.$refs.autoform.formdata.accountParentId = account.id;
					this.$refs.autoform.formdata.accountParentName = account.dicname;
				}else{
					let _that = this
					this.om.request("accountDict/findLevel1Account",{
						accountDictId: account.id
					}).then(res=>{
						if(res.succeed){
							var value = res.value
							_that.$refs.autoform.formdata.accountParentId = value.id;
							_that.$refs.autoform.formdata.accountParentName = value.dicname;
						}else{
							_that.om.toast(res.msg)
						}
					})
				}
			},
			clearAccount(){
				var formdata = this.$refs.autoform.formdata
				this.$refs.autoform.clear("title2", "accountId")					
				formdata.accountCode = ""
				formdata.accountParentId = ""
				formdata.accountParentName = ""
			},
			verifyStartTime(datetime){
				var currentTime = getDateStr(new Date())
				var endTime = this.formdata.endTime || currentTime
				
				if(datetime > endTime){
					return "开始时间不大于当前时间和结束时间"
				}else{
					return true
				}
				
			},
			verifyEndTime(datetime){
				var currentTime = getDateStr(new Date())
				var startime = this.formdata.startTime
				
				if(startime){
					if(datetime < startime){
						return "结束时间不小于开始时间"
					}else if(datetime > currentTime){
						return "结束时间不大于当前时间"
					}else{
						return true
					}
				}else if(datetime > currentTime){
					return "结束时间不大于当前时间"
				}else{
					return true
				}
			},
			initLoad(){
				let _that = this
				//交通工具
				_that.om.request('dictZero/getCombobox', {
					code: "37373"					
				}).then(res => {
					_that.$refs.autoform.changeEditor("title2", "vehicleTypeId", "data", configSelectData(res, "id", "dicname"))
					_that.$refs.autoform.initSelectIndex("title2", "vehicleTypeId")
				});
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				_that.bxUserId = e.bxUserId
				_that.initValue.partakeInstId = e.instId
				_that.initValue.partakeInstName = e.instName
				if(e.bxUserId && e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								changeEditor(_that.autoformdata[0].editors, "partakeInstId","link","../../public/search/search")
								changeEditor(_that.autoformdata[0].editors, "partakeInstId", "linkParam", {
									method:"post",
									namefield:"instname",
									searchfield:"instName",
									initload:true,
									url:"tinstitution/findNotBoard1",
									cachename:_that.instCacheKey,
									placeholder:"请输入机构名称"
								})
								changeEditor(_that.autoformdata[0].editors, "businessTravelDataId","link","../../public/search/search")
								changeEditor(_that.autoformdata[0].editors, "businessTravelDataId", "linkParam", {
									other:JSON.stringify({
										userId:_that.bxUserId,
										travelExpensesFormId:''
									}),
									namefield:"businessTravelCode",
									notefield:"cityName|address",
									initload:true,
									url:"travelExpensesForm/getDataList",
									cachename:_that.businessformCacheKey,
									placeholder:"请输入关键字查询"
								})
								changeEditor(_that.autoformdata[0].editors, "startTime","endField","endTime")
								changeEditor(_that.autoformdata[0].editors, "endTime","startField","startTime")
								changeEditor(_that.autoformdata[0].editors, "endTime","max", getDateStr(new Date()))
								
								changeEditor(_that.autoformdata[0].editors, "accountId","link","../../public/search/search")
								changeEditor(_that.autoformdata[0].editors, "accountId", "linkParam", {
									other:JSON.stringify({
										selectInBudget: true
									}),
									namefield:"dicname",
									initload:true,
									url:"accountDict/getCombobox",
									cachename:_that.accountCacheKey,
									placeholder:"请输入关键字查询"
								})
								
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", "1")
								if(_that.formdata.fileList) {
									var tempFileList = []
									var fileUrlList = _that.formdata.fileList.map(t=>{
										tempFileList.push(t.fileSaveName)
										return t.fileSaveName
									})
									changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
									changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
								}
								_that.initLoad()
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: _that.instCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.partakeInstId = d.id
							formdata.partakeInstName = d.instname
							uni.removeStorage({
								key: _that.instCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.businessformCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.businessTravelId = d.businessTravelId;
							formdata.businessTravelDataId = d.businessTravelDataId;
							formdata.businessTravelCode = d.businessTravelCode;
							formdata.planStartTime = d.planStartTime;
							formdata.planEndTime = d.planEndTime;
							formdata.startTime = d.planStartTime;
							formdata.endTime = d.planEndTime;
							formdata.cityName = d.cityName;
							formdata.address = d.address;
							formdata.vehicleTypeId = d.vehicleTypeId;
							formdata.vehicleTypeName = d.vehicleTypeName;
							_that.$refs.autoform.initSelectIndex("title2", "vehicleTypeId")
							uni.removeStorage({
								key: _that.businessformCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key:_that.accountCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata= _that.$refs.autoform.formdata
							formdata.accountId = d.id
							formdata.accountName = d.dicname
							_that.onSelectAccount(d)
							uni.removeStorage(_that.accountCacheKey)
						}
					}
				})
				
			}
		}
	}
</script>

<style>

</style>
